package com.hgd.security.evp.biz.biz;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hgd.security.evp.common.biz.BusinessBiz;
import com.hgd.security.evp.common.constances.CustomHttpStatus;
import com.hgd.security.evp.common.util.*;
import org.springframework.stereotype.Service;

import com.hgd.security.evp.biz.entity.EvpDict;
import com.hgd.security.evp.biz.mapper.EvpDictMapper;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 *
 * @author By Shang
 * @email ${email}
 * @version 2019-04-01 22:48:19
 */
@Service
public class EvpDictBiz extends BusinessBiz<EvpDictMapper,EvpDict> {

    /**
     * 分页获取列表
     * @param evpDict
     * @param page
     * @param rows
     * @return
     */
    public TableResultResponse<EvpDict> getDictList(EvpDict evpDict, Integer page, Integer rows) {
        Example example=new Example(EvpDict.class).selectProperties(
            "id",
            "dictCode",
            "dictValue",
            "dictLabel"
        );

        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isDeleted", BooleanUtil.BOOLEAN_FALSE);

        if(StringUtil.isNotBlank(evpDict.getDictLabel())){
            criteria.andLike("dictLabel", "%"+evpDict.getDictLabel()+"%");
        }

        example.setOrderByClause("dict_code");
        Page<Object> pageInfo = PageHelper.startPage(page, rows);
        List<EvpDict> evpDicts =
            this.selectByExample(example);
        if(EmptyUtil.isNotEmpty(evpDicts)){
            return new TableResultResponse<>(pageInfo.getTotal(), evpDicts);
        }

        return new TableResultResponse<>(0, new ArrayList<>());
    }

    /**
     * 添加单个对象
     * @param evpDict
     * @return
     */
    public ObjectResultResponse<Void> addDict(EvpDict evpDict) {
        ObjectResultResponse<Void> oResult=new ObjectResultResponse<>();

        check(evpDict,oResult,false);
        if(oResult.getStatus()== CustomHttpStatus.COMMON_ERROR){
            return oResult;
        }

        this.insertSelectiveBindCrt(evpDict);
        oResult.setMessage("添加部门成功");
        return oResult;
    }

    private void check(EvpDict evpDict, ObjectResultResponse<Void> oResult,boolean isupdate) {
    }

    /**
     * 更新单个对象
     * @param evpDict
     * @return
     */
    public ObjectResultResponse<Void> updateDict(EvpDict evpDict) {
        ObjectResultResponse<Void> oResult=new ObjectResultResponse<>();

        check(evpDict,oResult,true);
        if(oResult.getStatus()== CustomHttpStatus.COMMON_ERROR){
            return oResult;
        }

        this.updateSelectiveByIdBindUpd(evpDict);
        oResult.setMessage("编辑部门成功");
        return oResult;
    }

    /**
     * 删除单个对象
     * @param id
     * @return
     */
    public ObjectResultResponse<Void> deleteDict(Integer id) {
        ObjectResultResponse<Void> objectResultResponse=new ObjectResultResponse<>();

        EvpDict evpDict=new EvpDict();
        evpDict.setIsDeleted(BooleanUtil.BOOLEAN_TRUE);
        evpDict.setId(id);
        this.updateSelectiveByIdBindUpd(evpDict);

        objectResultResponse.setMessage("删除字典成功");
        return objectResultResponse;
    }
}